﻿using System;
using System.Windows;

namespace MyUtils.UI
{
	public static class MsgBox
	{
		private static readonly object _msgBoxLock = new object();

		public static void CatchToMsgbox(Action act)
		{
			try
			{
				act();
			}
			catch (Exception ex)
			{
				// Logger.Source.Exception(ex);
				lock (_msgBoxLock)
				{
					DispatchableObservable.DefaultDispatcherInvoke(exxObject =>
					{
						var exx = (Exception)exxObject;
						string queue = ex.GetType().Name;
						while (ex.InnerException != null)
						{
							ex = ex.InnerException;
							queue += "-> " + ex.GetType().Name;
						}
						lock (_msgBoxLock)
						{
							MessageBox.Show(exx.Message, queue, MessageBoxButton.OK, MessageBoxImage.Error);
						}
					}, ex);
				}
			}
		}
	}
}
